var mysql = require('mysql')
var config = require('../config')
var pool = mysql.createPool({
  host : config.database.HOST,
  user : config.database.USERNAME,
  password : config.database.PASSWORD,
  database : config.database.DATABASE
})
/**
 *
 * @param sql           sql语句
 * @param values        value属性值
 * @return {Promise<any>}   回调
 */
let query = function(sql, values){
  return new Promise((resolve, reject) =>{
    pool.getConnection(function(err, connection){
      if(err){
        resolve(err)
      } else {
        connection.query(sql, values, (err, rows) =>{
          if(err){
            reject(err)
          } else {
            resolve(rows)
          }
          connection.release()
        })
      }
    })
  })

}

/*query('select * from userinfo',null).then((rows)=>{
	console.log(rows)
},(err)=>{
	console.log(err)
})*/
module.exports = {query}
